package libs.yzx.zaza.acts;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

import java.io.File;

import libs.yzx.zaza.R;

/**
 * Created by Administrator on 2016/8/25
 */
public class BitmapDensityActivity extends AppCompatActivity{

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }


    private void fileSizeAndMemory(){
        /* 原图 150*150  , 5.82kb */
        /* 直接加载 , 内存占用0.5859kb ,  宽高不变*/
        /* 当inSampleSize = 2, 内存折半 , 宽高折半 , 等于3同理*/

        File image = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/a.jpg");
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 2;
        Bitmap bitmap = BitmapFactory.decodeFile(image.getAbsolutePath(),options);

        Toast.makeText(BitmapDensityActivity.this, bitmap.getRowBytes()/(float)1024 + "", Toast.LENGTH_SHORT).show();
        Toast.makeText(BitmapDensityActivity.this, options.outWidth +"  " + options.outHeight, Toast.LENGTH_SHORT).show();
    }



    /*
    * 不同的dpi包下, 对图片加载到内存的大小和宽高的影响
    * */
    private void sizeAndMemory(){
        /* densityDpi                                                                                  density */
        /* mdpi -> 160                      对应的px dp 转换就是 160/160 = 1*/
        /* hdpi -> 240                       对应的px dp 转换就是 240/160 = 1.5 */
        /* xhdpi -> 320                   对应的px dp 转换就是 300/160 = 2 */
        /* xxdpi -> 480                   对应的px dp 转换就是 480/160 = 3 */
        /* xxxhdpi -> 640             对应的px dp 转换就是 640/160 = 4 */

        /* 检测大小 和 图片到内存的宽高的变化     宽高 -> (内存宽高变成 :  原图宽高 / 对应包的dpi * 手机的dpi  )*/
        /* 放到 xxxhdpi 是512kb */  //    ( 792 / 640 * 480 )  *  ( 295 /640 *480 )  * ( 4 )
        /* 放到hdpi 是3650kb */       //   ( 792 / 240 * 480 )  *  ( 295 /240 *480 )  * ( 4 )
        //Bitmap xxxBmp = BitmapFactory.decodeResource(getResources(), R.mipmap.file_form);
        //Toast.makeText(BitmapDensityActivity.this, "" + (xxxBmp.getByteCount() / 1024), Toast.LENGTH_SHORT).show();


        /* 当图片放到没有后缀的drawable里边 ,  按照mdpi (160dpi) 计算*/
        BitmapFactory.Options o = new BitmapFactory.Options();
        Bitmap drawableBmp = BitmapFactory.decodeResource(getResources(), R.drawable.file_form , o);
        Toast.makeText(BitmapDensityActivity.this, o.outWidth+"   "+ o.outHeight, Toast.LENGTH_SHORT).show();
    }

}
